package com.song.osclient;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.song.osclient.qrcode.BitmapDecodeHandler;
import com.song.osclient.qrcode.Constant;
import com.song.osclient.qrcode.PreviewDecodeHandler;
import com.song.osclient.view.CameraView;
import com.song.osclient.view.PrintToast;
import com.song.osclient.view.ScreenOverlay;

/**
 * Created by Administrator on 2015/12/16.
 */
public class OptionScanActivity extends Activity {
    CameraView cameraView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_option_scan);
        cameraView = (CameraView) findViewById(R.id.cameraView);
        final ScreenOverlay overlay = (ScreenOverlay) findViewById(R.id.overlay);
        cameraView.setInitListener(new CameraView.InitListener() {
            @Override
            public void initSuccess() {
                cameraView.setPreviewCallback(new PreviewDecodeHandler(overlay.getPreviewRect(), handler));
            }
        });
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BitmapDecodeHandler().start(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/chart.png"), handler);
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == Constant.VALUE_DECODE_SUCCESS) {
                PrintToast.print((String) msg.obj);
                cameraView.release();
            } else if (msg.what == Constant.VALUE_DECODE_FAIL) {
                PrintToast.print("decode Fail");
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraView.release();
        cameraView = null;
    }
}
